package ferran.tomas.ta;

import java.util.ArrayList;

import ferran.tomas.dominio.User;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

public class NewContact extends Activity {
	EditText contact;
	private ServidorPrincipal sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.newcontact);

		contact = (EditText) findViewById(R.id.editContact);
		sp = ServidorPrincipal.getInstancia();

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		menu.setGroupVisible(R.id.grupo1, false);
		menu.setGroupVisible(R.id.grupo2, false);
		menu.setGroupVisible(R.id.grupo4, false);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {

		case R.id.back:
			back();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	public void back() {
		finish();
	}

	public void add_contact(View v) {

		String username = User.getInstancia().getNombre();
		String contacto = contact.getText().toString();

		if (sp.insertarContacto(username, contacto)) {
			User.getInstancia().getContactos().add(contacto);
			
			back();

		} else {
			// Mostrar mensaje de error
			String error = sp.getRespuestaServidorPrincipal();
			Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT)
					.show();
		}
	}
}
